package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.definterface.OnPYQCommentsDelegateListener;
import com.xmb.wechat.delegate.PYQAddCommentsDelegate;
import com.xmb.wechat.bean.PYQCommentsBean;
import com.xmb.wechat.widget.TitleLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.relation.ToMany;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/11 17:54
 */
public class WechatPYQCommentsActivity extends BaseActivity {

    private static final int REQUEST_CODE_ADD_COMMENTS  = 104;
    private static final int REQUEST_CODE_EDIT_COMMENTS = 105;

    @BindView(R2.id.title_layout)
    TitleLayout  mTitleLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;

    private boolean                isHasEdit;
    private PYQAddCommentsDelegate mDelegate;
    private int                    currentIndex;

    public WechatPYQCommentsActivity() {
        super(R.layout.activity_wechat_pyq_comments);
    }

    public static void start4Result(Activity ctx, ToMany<PYQCommentsBean> comments, int requestCode) {
        Intent intent = new Intent(ctx, WechatPYQCommentsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", comments);
        intent.putExtra("bundle", bundle);
        ctx.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mTitleLayout.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WechatPYQAddCommentsActivity.start4Result(WechatPYQCommentsActivity.this, null, REQUEST_CODE_ADD_COMMENTS);
            }
        });
        mDelegate = new PYQAddCommentsDelegate(this, new OnPYQCommentsDelegateListener() {
            @Override
            public void onItemClick(PYQCommentsBean entity) {
                currentIndex = mDelegate.getData().indexOf(entity);
                WechatPYQAddCommentsActivity.start4Result(WechatPYQCommentsActivity.this, entity, REQUEST_CODE_EDIT_COMMENTS);
            }
        });
        //传递数据
        Bundle bundle = getIntent().getBundleExtra("bundle");
        if (bundle != null) {
            ArrayList<PYQCommentsBean> comments = (ArrayList<PYQCommentsBean>) bundle.getSerializable("data");
            if (comments != null && comments.size() > 0)
                mDelegate.addData(comments);
        }
    }

    @OnClick(R2.id.btn_preview)
    public void onViewClicked() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        List<PYQCommentsBean> data = mDelegate.getData();
        bundle.putSerializable("data", (Serializable) data);
        intent.putExtra("bundle", bundle);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD_COMMENTS) {
            if (data == null)
                return;
            PYQCommentsBean entity = (PYQCommentsBean) data.getSerializableExtra("data");
            if (entity == null)
                return;
            mDelegate.additem(entity);
            isHasEdit = true;
        } else if (requestCode == REQUEST_CODE_EDIT_COMMENTS) {
            if (data == null)
                return;
            PYQCommentsBean entity = (PYQCommentsBean) data.getSerializableExtra("data");
            if (entity == null)
                return;
            mDelegate.removeItem(currentIndex);
            mDelegate.getData().add(currentIndex, entity);
            isHasEdit = true;
        }
    }

    @Override
    protected boolean isNeedShowLeaveHint() {
        return isHasEdit || mDelegate.hasDelete();
    }
}
